package com.extsci.webmail.config;

import com.extsci.webmail.common.type.MailProtocolType;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.mail.Session;
import java.util.Properties;

/**
 * 邮件服务器配置
 * @author ccoke
 */
@Configuration
public class MailConfig {
    private final String MAIL_TRANSPORT_PROTOCOL = "mail.transport.protocol";
    private final String MAIL_STORE_PROTOCOL = "mail.store.protocol";
    private final String MAIL_SMTP_HOST = "mail.smtp.host";
    private final String MAIL_SMTP_AUTH = "mail.smtp.auth";
    private final String MAIL_IMAP_HOST = "mail.imap.host";
    private final String MAIL_IMAP_AUTH = "mail.imap.auth";
    private final String TRUE = "true";
    
    @Value("${webmail.email-server.hostname}")
    private String hostname;

    @Bean(name = "sendSession")
    public Session getSendSession() {
        Properties properties = new Properties();
        properties.setProperty(MAIL_TRANSPORT_PROTOCOL, MailProtocolType.SMTP.type);
        properties.setProperty(MAIL_SMTP_HOST, hostname);
        properties.setProperty(MAIL_SMTP_AUTH, TRUE);
        return Session.getInstance(properties);
    }

    @Bean(name = "receiveSession")
    public Session getReceiveSession() {
        Properties properties = new Properties();
        properties.setProperty(MAIL_STORE_PROTOCOL, MailProtocolType.IMAP.type);
        properties.setProperty(MAIL_IMAP_HOST,  hostname);
        properties.setProperty(MAIL_IMAP_AUTH, TRUE);
        return Session.getInstance(properties);
    }
}
